﻿using RepositoryPattern.Infrastructure;
using RepositoryPattern.Models.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RepositoryPattern.Services
{
    public class CatalogService : ICatalogService, IDisposable
    {
        private IDALContext dalContext;
        public CatalogService(IDALContext context)
        {
            dalContext = context;
        }
        public List<Category> GetCategories()
        {
            return dalContext.CategoryRepostitory.All().ToList();
        }

        public List<Product> GetProducts()
        {
            return dalContext.ProductRepository.All().ToList();
        }

        public Product CreateProduct(string categoryName, string productName, int price)
        {
            var category = new Category { Name = categoryName };
            var product = new Product
            {
                Category = category,
                Name = productName,
                Price = price
            };
            dalContext.ProductRepository.Create(product);
            dalContext.SaveChanges();
            return product;
        }

        public void Dispose()
        {
            dalContext.Dispose();
        }
    }
}